//
//  HMVisitorView.swift
//  Weibo
//
//  Created by 吴少飞 on 16/9/20.
//  Copyright © 2016年 wushaofei. All rights reserved.
//

import UIKit

class HMVisitorView: UIView {

    //回调闭包
    var startLogging:(()->())?
    
    //旋转视图
    private lazy var rotateView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))

    //遮罩视图
    private lazy var shadeView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //中间图标
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //显示信息文字
    private lazy var messageLable :UILabel = {
        
        let Lab = UILabel()
        
        Lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        Lab.font = UIFont.systemFont(ofSize: 13)
        
        Lab.textColor = UIColor.darkGray
        
        //文字居中
        Lab.textAlignment = .center
        //自动换行
        Lab.numberOfLines = 0
        
        return Lab
    }()
    
    //登录按钮
    private lazy var loginBtn : UIButton = {
       
        let btn = UIButton()
        
        btn.addTarget(self, action: #selector(logingAction), for: .touchUpInside)
        btn.setTitle("登录", for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        //取消选中状态
        btn.adjustsImageWhenHighlighted = false
        
        return btn
    }()
    
    //注册按钮
    private lazy var regeditBtn : UIButton = {
        
        let btn = UIButton()
        
        btn.addTarget(self, action: #selector(logingAction), for: .touchUpInside)
        btn.setTitle("注册", for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        //取消选中状态
        btn.adjustsImageWhenHighlighted = false
        
        return btn
    }()
    
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK - 加载视图
    func setupUI(){
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)

        //添加子控件
        addSubview(rotateView)
        addSubview(iconView)
        addSubview(shadeView)
        addSubview(messageLable)
        addSubview(loginBtn)
        addSubview(regeditBtn)
        
        //设置约束
        rotateView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
            
        }
        
        
        iconView.snp_makeConstraints { (make) in
            
            make.center.equalTo(rotateView)
        }
        
        
        shadeView.snp_makeConstraints { (make) in
            
            make.center.equalTo(rotateView)
            
        }
        
        messageLable.snp_makeConstraints { (make) in
            
            make.top.equalTo(rotateView.snp_bottom)
            make.centerX.equalTo(rotateView)
            make.width.equalTo(224)
        }
        
        loginBtn.snp_makeConstraints { (make) in
            
            make.leading.equalTo(messageLable.snp_leading)
            make.top.equalTo(messageLable.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        regeditBtn.snp_makeConstraints { (make) in
            
            make.trailing.equalTo(messageLable.snp_trailing)
            make.top.equalTo(loginBtn)
            make.size.equalTo(loginBtn)
            
        }
        
    }
    //MARK -提供修改内容方法
    func replaceText(imageName:String?,messages:String?){
        
        if imageName != nil && messages != nil {
            
            iconView.image = UIImage(named: imageName!)
            messageLable.text = messages
            
            rotateView.isHidden = true
        }else{
            
            startAnimation()
        }
        
    }

    //MARK -开始动画
    func startAnimation(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //动画路径
        animation.toValue = 2 * M_PI
        //动画市场
        animation.duration = 15
        //重复次数
        animation.repeatCount = MAXFLOAT
        //失去焦点不移除动画
        animation.isRemovedOnCompletion = false
        rotateView.layer.add(animation, forKey: nil)
        
    }
    
    //MARK -- 按钮点击事件
    func logingAction() {
        
        startLogging!()
        
    }

}
